package com.hui.service.modules;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hui.entity.dto.OrderExtendDto;
import com.hui.entity.enums.AppHttpCodeEnum;
import com.hui.entity.enums.OrderLogEnum;
import com.hui.entity.enums.PayTypeEnum;
import com.hui.entity.vo.StoreOrderQueryVo;
import com.hui.handler.exception.SystemException;
import com.hui.model.order.ComputeOrderParam;
import com.hui.service.StoreOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Service
@Slf4j
public class OrderSupplyService {

    private final StoreOrderService storeOrderService;

    /**
     * 订单检测
     *
     * @param uid   uid
     * @param param ComputeOrderParam
     * @return map
     */
    public Map<String, Object> check(Long uid, ComputeOrderParam param) {
        Map<String, Object> map = new HashMap<>();
        //效验订单唯一key
        String key = param.getKey();
        if (StrUtil.isBlank(key)) {
            throw new SystemException(AppHttpCodeEnum.NO_PARAMETE_OPERATE.getCode(), "参数错误");
        }
        //获取订单信息
        StoreOrderQueryVo storeOrder = storeOrderService.getOrderInfo(key, uid);

        if (ObjectUtil.isNotNull(storeOrder)) {

            OrderExtendDto orderExtendDTO = OrderExtendDto.builder()
                    .key(key)
                    .orderId(storeOrder.getOrderId())
                    .build();
            map.put("status", OrderLogEnum.EXTEND_ORDER.getValue());
            map.put("result", orderExtendDTO);
            map.put("msg", OrderLogEnum.EXTEND_ORDER.getDesc());
        }
        return map;
    }


    /**
     * 支付
     *
     * @param map      map
     * @param orderId  订单号
     * @param uid      uid
     * @param payType  支付方式
     * @param from     来源
     * @param orderDTO orderDTO
     * @return map
     */
    public Map<String, Object> goPay(Map<String, Object> map, String orderId, Long uid, String payType,
                                     String from, OrderExtendDto orderDTO) {
        //订单支付使用
        switch (PayTypeEnum.toType(payType)) {
            /*
            //  微信支付
            case WEIXIN:
                Map<String,String> jsConfig = new HashMap<>();
                if(AppFromEnum.WEIXIN_H5.getValue().equals(from)){
                    WxPayMwebOrderResult wxPayMwebOrderResult = (WxPayMwebOrderResult)weixinPayService
                            .unifyPay(orderId,from, BillDetailEnum.TYPE_3.getValue(),"H5商品购买");

                    log.info("wxPayMwebOrderResult:{}",wxPayMwebOrderResult);
                    jsConfig.put("mweb_url",wxPayMwebOrderResult.getMwebUrl());
                    orderDTO.setJsConfig(jsConfig);
                    map.put("result",orderDTO);
                    map.put("status","WECHAT_H5_PAY");
                    map.put("payMsg","订单创建成功");
                    return map;
                } else if(AppFromEnum.ROUNTINE.getValue().equals(from)){
                    map.put("status","WECHAT_PAY");
                    WxPayMpOrderResult wxPayMpOrderResult = (WxPayMpOrderResult)weixinPayService
                            .unifyPay(orderId,from, BillDetailEnum.TYPE_3.getValue(),"小程序商品购买");
                    jsConfig.put("appId",wxPayMpOrderResult.getAppId());
                    jsConfig.put("timeStamp",wxPayMpOrderResult.getTimeStamp());
                    jsConfig.put("paySign",wxPayMpOrderResult.getPaySign());
                    jsConfig.put("nonceStr",wxPayMpOrderResult.getNonceStr());
                    jsConfig.put("package",wxPayMpOrderResult.getPackageValue());
                    jsConfig.put("signType",wxPayMpOrderResult.getSignType());
                    orderDTO.setJsConfig(jsConfig);
                    map.put("payMsg","订单创建成功");
                    map.put("result",orderDTO);
                    return map;
                }else if(AppFromEnum.APP.getValue().equals(from)){//app支付
                    map.put("status","WECHAT_APP_PAY");
                    WxPayAppOrderResult wxPayAppOrderResult = (WxPayAppOrderResult)weixinPayService
                            .unifyPay(orderId,from, BillDetailEnum.TYPE_3.getValue(),"APP商品购买");
                    jsConfig.put("appid",wxPayAppOrderResult.getAppId());
                    jsConfig.put("partnerid",wxPayAppOrderResult.getPartnerId());
                    jsConfig.put("prepayid",wxPayAppOrderResult.getPrepayId());
                    jsConfig.put("package",wxPayAppOrderResult.getPackageValue());
                    jsConfig.put("noncestr",wxPayAppOrderResult.getNonceStr());
                    jsConfig.put("timestamp",wxPayAppOrderResult.getTimeStamp());
                    jsConfig.put("sign",wxPayAppOrderResult.getSign());
                    orderDTO.setJsConfig(jsConfig);
                    map.put("result",orderDTO);
                    map.put("payMsg","订单创建成功");
                    return map;
                }else{//公众号
                    map.put("status","WECHAT_PAY");
                    WxPayMpOrderResult wxPayMpOrderResult = (WxPayMpOrderResult)weixinPayService
                            .unifyPay(orderId,from, BillDetailEnum.TYPE_3.getValue(),"公众号商品购买");

                    log.info("WxPayMpOrderResult:{}",wxPayMpOrderResult);

                    jsConfig.put("appId",wxPayMpOrderResult.getAppId());
                    jsConfig.put("timestamp",wxPayMpOrderResult.getTimeStamp());
                    jsConfig.put("nonceStr",wxPayMpOrderResult.getNonceStr());
                    jsConfig.put("package",wxPayMpOrderResult.getPackageValue());
                    jsConfig.put("signType",wxPayMpOrderResult.getSignType());
                    jsConfig.put("paySign",wxPayMpOrderResult.getPaySign());
                    orderDTO.setJsConfig(jsConfig);
                    map.put("result",orderDTO);
                    map.put("payMsg","订单创建成功");

                    return map;
                }*/
            case YUE:
                storeOrderService.yuePay(orderId, uid);
                map.put("payMsg", "余额支付成功");
                return map;

          /*
            //积分兑换成功
            case INTEGRAL:
                storeOrderService.integralPay(orderId,uid);
                map.put("payMsg","积分兑换成功");
                return map;*/
            default:
        }

        map.put("payMsg", "订单生成失败");
        return map;
    }
}
